package com.ddx.controller;

import com.ddx.common.exception.ExceptionUtil;
import com.ddx.common.exception.ResultModel;
import com.ddx.dto.ShoppingAttributeVo;
import com.ddx.dto.ShoppingProductVo;
import com.ddx.dto.ShortVideoDto;
import com.ddx.pojo.ShoppingSpec;
import com.ddx.service.IShoppingProductService;

import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 前端首页
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping({"/app/product/"})
public class ProductController {

    @Autowired
    IShoppingProductService shoppingProductService;

    private static Logger logger = LoggerFactory.getLogger(ProductController.class);
    
    /**
     * 商品详情
     * @param request
     * @param model
     * @return
     */
    @RequestMapping({"/detail.do"})
    public ModelAndView detail(HttpServletRequest request, ModelMap model) {
        ModelAndView mv = new ModelAndView("/app/classify/detail");
        int productId = Integer.parseInt(request.getParameter("productId"));
        ShoppingProductVo shoppingProduct = shoppingProductService.productDetail(productId);
        model.addAttribute("product", shoppingProduct);
       /* List<ShoppingAttributeVo> specList = shoppingProductService.attributeInfo(productId);
        model.addAttribute("specList", specList);*/
        return mv;
    }
    
    @ApiOperation(value = "出售中,已下架 的商品集合list【根据status状态区分商品状态 0 正常 1下架】", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/sell", method = { RequestMethod.POST })
    public ResultModel sell(@RequestBody ShoppingProductVo shoppingProductVo, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            resultModel.setData(shoppingProductService.sell(shoppingProductVo));
        } catch (Exception e) {
            logger.error("精彩页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    
    /**
     * 根据视频详情返回结果提供的商品id-跳转商品详情页
     */
    @ApiOperation(value = "根据商品id-跳转商品详情页", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/detailById", method = { RequestMethod.POST })
    public ResultModel detailById(@RequestBody ShoppingProductVo shoppingProductVo, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            resultModel.setData(shoppingProductService.detailById(shoppingProductVo));
        } catch (Exception e) {
            logger.error("精彩页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

}
